// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PoseDiff.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Numerics
{
    using System;
    using System.Runtime.Serialization;
    using ShoNS.Array;

    /// <summary>
    /// A class that provides methods to compute pose difference
    /// and related properties
    /// </summary>
    [Serializable]
    public class PoseDiff : DoubleArray
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PoseDiff" /> class.
        /// Given two frames represented by two homogeneous transform
        /// matrices, this constructor computes their difference in terms
        /// in translation and rotation.
        /// The reference frame is the one used by frame1.
        /// The representation of the orientation difference is based
        /// on the AxisAngle class in our codebase.
        /// In essence, this class is a 6 dimensional single column vector.
        /// [TX, TY, TZ, RX, RY, RZ].  The first three numbers TX, TY, TZ are the translational difference.
        /// The last three numbers RX, RY, RZ are the rotational difference. They are essentially
        /// the Axis part of the AxisAngle representation of the transform matrix from frame1 to
        /// frame2, scaled up by the Angle value of the corresponding rotation.
        /// </summary>
        /// <param name="frame1">First frame</param>
        /// <param name="frame2">Second frame</param>
        //// Wiki page http://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation
        public PoseDiff(Matrix4 frame1, Matrix4 frame2)
            : base(6, 1)
        {
            // compute the frame2 in frame1
            // each column of diffMatrix represents each axis of frame2 in frame1
            Matrix4 diffMatrix = HomogenousTransform.Inverse(frame1) * frame2;
            HomogenousTransform.OrthogonalizeRotationalMatrix(diffMatrix);
            DoubleArray diff = new DoubleArray(6, 1);

            AxisAngle diffAxisAngle = HomogenousTransform.ToAxisAngle(diffMatrix);
            this[0, 0] = diffMatrix[0, 3];
            this[1, 0] = diffMatrix[1, 3];
            this[2, 0] = diffMatrix[2, 3];
            this[3, 0] = diffAxisAngle.Angle * diffAxisAngle.Axis.X;
            this[4, 0] = diffAxisAngle.Angle * diffAxisAngle.Axis.Y;
            this[5, 0] = diffAxisAngle.Angle * diffAxisAngle.Axis.Z;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PoseDiff" /> class.
        /// Constructor for serialization
        /// </summary>
        /// <param name="info">Serialization information</param>
        /// <param name="context">The context</param>
        protected PoseDiff(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// Get the translational difference from pose difference
        /// </summary>
        /// <returns>The translational difference</returns>
        public DoubleArray TranslationalDifference()
        {
            return this.GetSlice(0, 2, 0, 0);
        }

        /// <summary>
        /// Get the rotational difference from pose difference
        /// </summary>
        /// <returns>The rotational difference</returns>
        public DoubleArray RotationalDifference()
        {
            return this.GetSlice(3, 5, 0, 0);
        }
    }
}
